package com.qdlc.p2p.biz.modelbuidler;

import com.qd.p2p.sina.pay.enums.IdentityType;
import com.qdlc.p2p.common.constant.Summary;
import com.qdlc.p2p.common.constant.TradeCode;
import com.qdlc.p2p.common.constant.TradeStatus;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.OrderNoUtils;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class TppTradeLogBuilder {

    private TppTradeLog tppTradeLog;

    /**
     * 托管账户ID
     */
    private String tppUserCustId;

    /**
     * 收款用户账户ID
     */
    private String toTppUserCustId;

    /**
     * 托管账户类型
     */
    private IdentityType identityType;

    /**
     * 交易金额 - 平台传输
     */
    private Double money;

    /**
     * 交易金额 - 第三方回调
     */
    private Double amount;

    /**
     * 订单号
     */
    private String orderNo;

    /**
     * 交易状态
     */
    private TradeStatus tradeStatus;

    /**
     * 交易类型
     */
    private TradeCode tradeType;

    /**
     * 业务类型 tender/loan 等
     */
    private String serviceType;

    /**
     * 借款标Id
     */
    private long borrowId;

    /**
     * 还款Id
     */
    private long borrowRepaymentId;

    /**
     * 分账信息
     */
    private String splitList;

    /**
     * 备注 最好是 Json字符串
     */
    private String memo;

    /**
     * 添加时间
     */
    private Date addTime;

    /**
     * 交易时间
     */
    private Date tradeTime;

    /**
     * 新浪状态
     */
    private String sinaStatus;

    /**
     * 备注 最好是 Json字符串
     */
    private String remark;

    /** 摘要 */
    private Summary summary;

    /** 关联订单号 */
    private String associateOrderId;

    public TppTradeLogBuilder setSinaStatus(String sinaStatus) {
        this.sinaStatus = sinaStatus;
        return this;
    }

    public TppTradeLogBuilder setTppTradeLog(TppTradeLog tppTradeLog) {
        this.tppTradeLog = tppTradeLog;
        return this;
    }

    public TppTradeLogBuilder setTppUserCustId(String tppUserCustId) {
        this.tppUserCustId = tppUserCustId;
        return this;
    }

    public TppTradeLogBuilder setToTppUserCustId(String toTppUserCustId) {
        this.toTppUserCustId = toTppUserCustId;
        return this;
    }

    public TppTradeLogBuilder setIdentityType(IdentityType identityType) {
        this.identityType = identityType;
        return this;
    }

    public TppTradeLogBuilder setMoney(double money) {
        this.money = money;
        return this;
    }

    public TppTradeLogBuilder setAmount(double amount) {
        this.amount = amount;
        return this;
    }

    public TppTradeLogBuilder setOrderNo(String orderNo) {
        this.orderNo = orderNo;
        return this;
    }

    public TppTradeLogBuilder setTradeStatus(TradeStatus tradeStatus) {
        this.tradeStatus = tradeStatus;
        return this;
    }

    public TppTradeLogBuilder setTradeType(TradeCode tradeType) {
        this.tradeType = tradeType;
        return this;
    }

    public TppTradeLogBuilder setServiceType(String serviceType) {
        this.serviceType = serviceType;
        return this;
    }

    public TppTradeLogBuilder setBorrowId(long borrowId) {
        this.borrowId = borrowId;
        return this;
    }

    public TppTradeLogBuilder setBorrowRepaymentId(long borrowRepaymentId) {
        this.borrowRepaymentId = borrowRepaymentId;
        return this;
    }

    public TppTradeLogBuilder setSplitList(String splitList) {
        this.splitList = splitList;
        return this;
    }

    public TppTradeLogBuilder setMemo(String memo) {
        this.memo = memo;
        return this;
    }

    public TppTradeLogBuilder setAddTime(Date addTime) {
        this.addTime = addTime;
        return this;
    }

    public TppTradeLogBuilder setTradeTime(Date tradeTime) {
        this.tradeTime = tradeTime;
        return this;
    }

    public TppTradeLogBuilder setSummary(Summary summary) {
        this.summary = summary;
        return this;
    }

    public TppTradeLogBuilder setRemark(String remark) {
        this.remark = remark;
        return this;
    }

    public TppTradeLogBuilder setAssociateOrderId(String associateOrderId) {
        this.associateOrderId = associateOrderId;
        return this;
    }

    private TppTradeLogBuilder() {
        tppTradeLog = new TppTradeLog();
    }

    public static TppTradeLogBuilder newBuilder() {
        return new TppTradeLogBuilder();
    }

    public TppTradeLog build() {
        checkNull();
        tppTradeLog.setTppUserCustId(tppUserCustId);
        tppTradeLog.setToTppUserCustId(toTppUserCustId);
        tppTradeLog.setIdentityType(identityType.name());
        tppTradeLog.setMoney(money);
        tppTradeLog.setTradeType(tradeType.toString());
        tppTradeLog.setTradeStatus(tradeStatus.getStatus());
        tppTradeLog.setServiceType(summary.getSummaryType());
        tppTradeLog.setMemo(memo);
        tppTradeLog.setBorrowId(borrowId);
        if (orderNo == null) {
            tppTradeLog.setOrderNo(OrderNoUtils.getSerialNumber());
        } else {
            tppTradeLog.setOrderNo(orderNo);
        }
        tppTradeLog.setAddTime(DateUtil.getNow());
        tppTradeLog.setRemark(remark);
        tppTradeLog.setAssociateOrderId(associateOrderId);
        tppTradeLog.setBorrowRepaymentId(borrowRepaymentId);
        return tppTradeLog;
    }

    public static TppTradeLogBuilder newBuilderForTradeCode1001() {
        TppTradeLogBuilder builder = newBuilder();
        builder.tradeType = TradeCode.C1001;
        builder.tradeStatus = TradeStatus.TRADE_STATUS_WAIT_PAY;
        return builder;
    }

    public static TppTradeLogBuilder newBuilderForTradeCode2001() {
        TppTradeLogBuilder builder = newBuilder();
        builder.tradeType = TradeCode.C2001;
        builder.tradeStatus = TradeStatus.TRADE_STATUS_WAIT_PAY;
        return builder;
    }

    /**
     * 红包兑现
     */
    public static TppTradeLogBuilder newBuilderForRedEnvelopeCollect() {
        TppTradeLogBuilder builder = newBuilder();
        builder.tradeType = TradeCode.C1003;
        builder.addTime = DateUtil.getNow(); // 还款时间
        builder.tradeStatus = TradeStatus.TRADE_STATUS_WAIT_PAY;
        builder.amount = 0d;// 交易金额
        builder.tppUserCustId = SystemConfigHelper.getValue(SystemConfigHelper.Nid.MEMBER_EMAIL); // 平台
        builder.identityType = IdentityType.EMAIL;
        return builder;
    }

    /**
     * 还款代收订单(个人)
     */
    public static TppTradeLogBuilder newBuilderForRepayCollect(boolean isPerson) {
        TppTradeLogBuilder builder = newBuilder();
        builder.addTime = DateUtil.getNow(); // 还款时间
        builder.amount = 0d;// 交易金额
        builder.summary = Summary.REPAY; // 还款
        if (isPerson) {
            builder.identityType = IdentityType.UID;
        } else {
            builder.identityType = IdentityType.EMAIL;
        }
        builder.tradeStatus = TradeStatus.TRADE_STATUS_WAIT_PAY;
        builder.splitList = "";
        return builder;
    }


    /**
     * 还款代收订单(个人)
     */
    public static TppTradeLogBuilder newBuilderForRepayPay() {
        TppTradeLogBuilder builder = newBuilder();
        builder.addTime = DateUtil.getNow(); // 还款时间
        builder.amount = 0d;// 交易金额
        builder.summary = Summary.REPAY; // 还款
        builder.identityType = IdentityType.UID;
        builder.tradeStatus = TradeStatus.TRADE_STATUS_WAIT_PAY;
        return builder;
    }

    private void checkNull() {
        List<String> emptyFields = new ArrayList<String>();
        if (StringUtils.isBlank(tppUserCustId)) {
            emptyFields.add("tppUserCustId");
        }
        if (money == null) {
            emptyFields.add("money");
        }
        if (amount == null) {
            emptyFields.add("amount");
        }
        if (identityType == null) {
            emptyFields.add("identityType");
        }
        if (tradeType == null) {
            emptyFields.add("tradeType");
        }
        if (tradeStatus == null) {
            emptyFields.add("tradeStatus");
        }
        if (summary == null) {
            emptyFields.add("summary");
        }
        if (!emptyFields.isEmpty()) {
            throw new IllegalArgumentException("TppTradeLog对象缺少参数:" + Arrays.toString(emptyFields.toArray()));
        }
    }
}